//
//  XiangLiTableViewCell.m
//  Cunli
//
//  Created by 湛亚 on 15/9/11.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "XiangLiTableViewCell.h"
#import "UIImageView+EMWebCache.h"
#import "PointsManager.h"
#import "ConvertToCommonEmoticonsHelper.h"
#import "LinLi.h"
#import "TTTAttributedLabel.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@implementation XiangLiTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier andLinLi:(LinLi *)linLi{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _headImg=[[UIImageView alloc]init];
        _headImg.frame=CGRectMake(10, 10, 40, 40);
        _headImg.layer.cornerRadius = 20;
        _headImg.clipsToBounds = YES;
        _headImg.userInteractionEnabled=YES;
        if ([PointsManager isDefaultTouXiangWithUrlstring:linLi.txpic]) {
            _headImg.image=[UIImage imageNamed:@"default_touxiang.png"];
            
        }else{
            [_headImg sd_setImageWithURL:[NSURL URLWithString:linLi.txpic] placeholderImage:[UIImage imageNamed:@"default_touxiang.png"]];
             
        }
        [self.contentView addSubview:_headImg];
    
        _nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 4, 22*(linLi.truename.length), 30)];
        _nameLabel.text=linLi.truename;
        _nameLabel.userInteractionEnabled=YES;
        _nameLabel.textColor=[UIColor colorWithRed:18/255.0 green:133/255.0 blue:210/255.0 alpha:1];
        [self.contentView addSubview:_nameLabel];
        
        UILabel *timeLab=[[UILabel alloc]initWithFrame:CGRectMake(60, 35, 120, 15)];
        timeLab.textColor=[UIColor grayColor];
        timeLab.text=linLi.addtime;
        timeLab.font=[UIFont systemFontOfSize:14];
        [self.contentView addSubview:timeLab];
        
        float height=[self heightWithString:linLi.introduce];
        NSString *contentText = [ConvertToCommonEmoticonsHelper
                                  convertToSystemEmoticons:linLi.introduce];
        _contentLabel=[PointsManager createWithframe:CGRectMake(10, 57, SCREEN_WIDTH-20, height) andString:contentText];
        [self.contentView addSubview:_contentLabel];
        
        height=height+60;
        float imgWidth=(SCREEN_WIDTH-40)/3.0;
        height=height+(linLi.pics.count+2)/3*(imgWidth-5);
        float kongGeWidth=(SCREEN_WIDTH-160)/4;
        
        UIImageView *helpImgPic=[[UIImageView alloc]initWithFrame:CGRectMake(kongGeWidth+12, height+4, 43, 15)];
        helpImgPic.image=[UIImage imageNamed:@"评论.png"];
        [self.contentView addSubview:helpImgPic];
        
        UILabel *helpCountLab=[[UILabel alloc]initWithFrame:CGRectMake(kongGeWidth+56, height+4, 25, 14)];
        helpCountLab.textColor=[UIColor grayColor];
        helpCountLab.text=[NSString stringWithFormat:@"%d",linLi.commentcount];
        helpCountLab.font=[UIFont systemFontOfSize:14];
        [self.contentView addSubview:helpCountLab];
        
        _zanImg=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-kongGeWidth-70, height+3, 33, 14)];
        _zanImg.image=[UIImage imageNamed:@"赞.png"];
        _zanImg.userInteractionEnabled=YES;
        [self.contentView addSubview:_zanImg];
        
        _zanCountLab=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-kongGeWidth-36, height+3, 25, 14)];
        _zanCountLab.textColor=[UIColor grayColor];
        _zanCountLab.text=[NSString stringWithFormat:@"%d",linLi.look];
        _zanCountLab.font=[UIFont systemFontOfSize:14];
        [self.contentView addSubview:_zanCountLab];
        
        UIView *sperView=[[UIImageView alloc]initWithFrame:CGRectMake(0, height-3,SCREEN_WIDTH, 1)];
        sperView.backgroundColor=[UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
        [self.contentView addSubview:sperView];
        
        UIView *sperView1=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2, height+1,1, 20)];
        sperView1.backgroundColor=[UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
        [self.contentView addSubview:sperView1];
    }
    return self;
}

-(float)heightWithString:(NSString *)introduce{
    
    CGRect rect = [introduce boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil];
    CGFloat height = ceilf(rect.size.height);
    return height + 10;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
